package com.ailink.foodshop.exception;

import com.ailink.foodshop.config.ProjectConfig;
import com.ailink.foodshop.enums.ResultEnum;
import com.ailink.foodshop.utils.ResultVoUtil;
import com.ailink.foodshop.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author lh
 * @date 2017/12/21 00:00
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @Autowired
    private ProjectConfig config;

//    @ExceptionHandler(value = Exception.class)
    @ExceptionHandler(value = SysException.class)
    @ResponseBody
    public ResultVo handle(Exception e) {
        if (e instanceof SysException) {
            SysException sysException = (SysException) e;
            return ResultVoUtil.error(sysException.getCode(), sysException.getMessage());
        } else {
//            return ResultVoUtil.error(ResultEnum.UNKNOWN_ERROR.getCode(), ResultEnum.UNKNOWN_ERROR.getMessage());
            return ResultVoUtil.error(ResultEnum.UNKNOWN_ERROR.getCode(), e.getMessage());
        }
    }

    //拦截登录异常
    @ExceptionHandler(value = SellerAuthorizeException.class)
    public ModelAndView handlerSellerAuthorizeException() {
        return new ModelAndView("redirect:".
                concat(config.getSell()).
                concat("/seller/user/login?"));
    }

    @ExceptionHandler(value = ResponseBlankException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public void handleResponseBlankException() {

    }
}
